package tech.aistar.day10;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/3/19 2:08 下午
 */
public class RemoveDemo {
    public static void main(String[] args) {
        List<Book> list = new ArrayList<>();

        Book b1 = new Book(5,"1005","西游记1",600.0d);
        Book b2 = new Book(6,"1006","西游记",600.0d);
        Book b3 = new Book(7,"1007","西游记",600.0d);
        Book b4 = new Book(8,"1008","西游记",600.0d);
        Book b5 = new Book(9,"1009","西游记2",600.0d);

        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        list.add(b5);

        removeAll(list,"西游记");

        for (Book book : list) {
            System.out.println(book);
        }
    }

    /**
     * 根据图书的名称进行删除
     * @param books
     * @param bookName
     */
    public static void removeAll(List<Book> books, String bookName){
//        for (int i = 0; i < books.size(); i++) {
//            if(books.get(i).getBookName().equals(bookName)){
//                //books.remove(i);
//                books.remove(books.get(i));
//                //下标的前移
//                i--;
//            }
//        }

//        for(int i=books.size()-1;i>=0;i--){
//            if(books.get(i).getBookName().equals(bookName)){
//                books.remove(i);
//            }
//        }

        //演示for循环的只读性

        //java.util.ConcurrentModificationException - 抛出并发修改的异常...
//        for (Book book : books) {
//            if(book.getBookName().equals(bookName)){
//                books.remove(book);
//                System.out.println("===>>>>");
//            }
      //  }

        Iterator<Book> iter = books.iterator();
        while(iter.hasNext()){
            Book bk = iter.next();
            if(bk.getBookName().equals(bookName)){
                //books.remove(bk);//java.util.ConcurrentModificationException - 抛出并发修改的异常...
                iter.remove();

            }
        }
    }

}
